﻿Imports Zytonic_Framework.Math
Imports System.Windows.Forms
Imports Go_Gadget.Extentions
Imports System.IO
Imports System

Module DockModule

#Region "Properties"

    Public Property Color As Color
    Public Property IconColor As Color

    Public Property Properties As New MyProperties

#End Region

#Region "Wallpaper Thread"
    Public MainThread As New System.Threading.Thread(AddressOf WallpaperThread) With {.IsBackground = True}
    Private BackgroundFile As String, BackgroundDate As DateTime

    Private Sub WallpaperThread()
        Do
            Dim diffInSeconds As Long = _
                If(BackgroundDate = Nothing, 0, DateAndTime.DateDiff(DateInterval.Second, BackgroundDate, File.GetLastWriteTime(BackgroundFile)))

            Dim CurrentWallpaper As String = MyProperties.Wallpaper

            If (diffInSeconds <> 0) Or (CurrentWallpaper <> BackgroundFile) Then
                If BackgroundFile = Nothing Then BackgroundFile = MyProperties.Wallpaper

                Try
                    Using Image As New Drawing.Bitmap(BackgroundFile)
                        Dim NewColor As Color = ListSameColours(Image)
                        If Color <> NewColor Then
                            Application.Current.Dispatcher.Invoke(Sub() ChangeDockColor(NewColor))
                            BackgroundDate = System.IO.File.GetLastWriteTime(BackgroundFile)
                            BackgroundFile = CurrentWallpaper
                        End If
                    End Using
                Catch ex As ArgumentException
                    'Wait for the background to change...
                    System.Threading.Thread.Sleep(100)
                    Continue Do
                End Try

            End If
            System.Threading.Thread.Sleep(100)
        Loop
    End Sub

    Function ListSameColours(ByVal Image As Drawing.Bitmap) As Color
        Dim FoundWallpaper As Boolean = False
        Dim ListColors As New List(Of String)
        Dim Popular As New List(Of Long)

        If Image.Height > 250 And Image.Width > 250 Then
            FoundWallpaper = True
            Dim CreateCanvas As New Drawing.Bitmap(60, 20)
            Dim TheGraphics As Drawing.Graphics = Drawing.Graphics.FromImage(CreateCanvas)
            TheGraphics.DrawImage(Image, 0, 0, 60, 20)
            Image = CreateCanvas
        End If
        Dim b As New Drawing.Bitmap(Image)
        For x = If(FoundWallpaper = True, 0, b.Size.Width / 2.5 * 1) To If(FoundWallpaper = True, b.Size.Width - 1, b.Size.Width / 2.5 * 3)
            For y = If(FoundWallpaper = True, 0, b.Size.Height / 2.5 * 1) To If(FoundWallpaper = True, b.Size.Height - 1, b.Size.Height / 2.5 * 3)
                If Not ListColors.IndexOf(b.GetPixel(x, y).ToArgb.ToString) = -1 Then
                    Popular.Add(ListColors.IndexOf(b.GetPixel(x, y).ToArgb.ToString))
                Else
                    ListColors.Add(b.GetPixel(x, y).ToArgb.ToString)
                End If
            Next y
        Next
        Dim Mean As Long
        For res = 0 To Popular.Count - 1
            Mean += Popular(res)
        Next
        If Mean <> 0 Then Mean = Mean / Popular.Count
        Dim C As Color = Drawing.Color.FromArgb(ListColors(Mean)).toWPF
        C.A = PercentUsed(255, 67.5)
        Return C
    End Function

    Function FindPinIndex(Filename As String) As Integer
        For i As Integer = 0 To MyProperties.DockWindow.PinStackPanel.Children.Count - 1
            Dim PinControl As PinControl = MyProperties.DockWindow.PinStackPanel.Children(i)
            If PinControl.PinData.Filename.ToLower = Filename.ToLower Then
                Return i
            End If
        Next
        Return Nothing
    End Function

#End Region

End Module
